/// Appendix ITT Specifications

* Get data

use "data/pseudodata/userlevel_pseudodata.dta", clear

* Note: I did not generate two-month outcomes in the pseudodata but they follow the same logic
*replace bound 1 to 100 for ITT graphs
foreach var of varlist bound anybound* threemonther threemonthcrer { // twomonther twomonthcrer {
	replace `var' = 100 if `var'==1
}
/*
*** hugo and any insurance ITT (two months)
eststo clear

local outcomes ///
"bound daysnonzeroreserve60 totdayscoverage60 twomonther anybound7 daysnonzeroreserve60cr totdayscoverage60cr twomonthcrer"

local i=1		
foreach var of varlist `outcomes' {
	eststo est`i':  quietly reg `var' daily, r
	quietly summ `var'
	estadd scalar mean = r(mean)
	local i=`i'+1
}

esttab est1 est2 est3 est4 using "output/tables/ITT daily contract alltwomo.tex", ///
prehead("\begin{tabular}{l*{5}{c}} \hline\hline") ///
posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel A: Insurance through Experiment}} \\\\[-1ex]") ///
fragment ///
replace label nomtitle unstack ///
stats(N, fmt(%11.0gc) labels("N")) ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-.9mm}")))  /// 
	mgroups("Take-Up" "Days with Coverage" "Days Insured" "Insured End of Study", ///
		pattern(1 1 1 1))

esttab est5 est6 est7 est8 using "output/tables/ITT daily contract alltwomo.tex", ///
posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel B: Any Insurance}} \\\\[-1ex]") ///
fragment append ///
nomtitles nomtitle nonumbers nolines ///
prefoot("\hline") postfoot("\hline\hline \end{tabular}") ///
label unstack ///
stats(N, fmt(%11.0gc) labels("N")) ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-.9mm}")))
*/
	
**** Regress pay-as-you-go offer separately by price treatment group 
gen loweprice = price=="low"
gen baseprice = price=="base"
gen highprice = price=="high"

label variable loweprice "PAYG, Low Price"
label variable baseprice "PAYG, Base Price"
label variable highprice "PAYG, High Price"

eststo clear

local outcomes ///
"bound daysnonzeroreserve90 totdayscoverage90 threemonther anybound7 daysnonzeroreserve90cr totdayscoverage90cr threemonthcrer"
local i=1
foreach var of varlist `outcomes' {
	eststo est`i':  reg `var' loweprice baseprice highprice, r
	quietly summ `var'
	estadd scalar mean = r(mean)
	local i = `i'+1
}			
		
esttab est1 est2 est3 est4 using "output/tables/ITT daily contract pricefac.tex", ///
prehead("\begin{tabular}{l*{5}{c}} \hline\hline") ///
posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel A: Insurance through Experiment}} \\\\[-1ex]") ///
fragment ///
replace label nomtitle unstack ///
stats(N, fmt(%11.0gc) labels("N")) ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-1mm}")))   /// 
	mgroups("Take-Up" "Days with Coverage" "Days Insured" "Insured End of Study", ///
		pattern(1 1 1 1))

esttab est5 est6 est7 est8 using "output/tables/ITT daily contract pricefac.tex", ///
posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel B: Any Insurance}} \\\\[-1ex]") ///
fragment append ///
nomtitles nomtitle nonumbers nolines ///
prefoot("\hline") postfoot("\hline\hline \end{tabular}") ///
label unstack ///
stats(N, fmt(%11.0gc) labels("N")) ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-.9mm}")))


********************************************************************************
*** Robustness to controlling for covariates
	
set seed 8675309

** LASSO with a good number of covariates
*** again but LASSO
local outcomes ///
"bound anybound7 daysnonzeroreserve90 daysnonzeroreserve90cr"
local allcontrols = "totalagentpremium makemodelyearvalue_private car_year age income_insight_score vantage_v4_score totinq totalrevlimit cardlimit cardbalance isconstrained autoloan autoloanamount medcoll nonmedcoll clar_totinq clar_limits clar_balance"

* Set missing Experian and Clarity variables to zeroes 
local evartozero = "totinq totalrevlimit cardlimit cardbalance isconstrained autoloan autoloanamount medcoll nonmedcoll"
foreach var of varlist `evartozero' {
	replace `var'=0 if mi(`var')
}
local cvars = "clar_totinq clar_limits clar_balance"
foreach var of varlist `cvars' {
	replace `var'=0 if mi(`var')
}

dsregress bound daily, controls(`allcontrols') vce(robust)
local i=1		
foreach var of varlist `outcomes' {
	eststo est`i': quietly reg `var' daily, r
	estadd local conts = "None"
	local i=`i'+1
	eststo est`i': quietly reg `var' daily `allcontrols', r
	estadd local conts = "All"
	local i=`i'+1
	eststo est`i': quietly dsregress `var' daily, controls(`allcontrols') vce(robust)
	estadd local conts = "Plug-in LASSO"
	estadd local selected = "`e(k_controls_sel)'"
	local i=`i'+1
}

esttab est1 est2 est3   using "output/tables/ITT daily contract controls.tex", ///
prehead("\begin{tabular}{l*{5}{c}} \hline\hline") ///
posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel A: Take-Up through Experiment}} \\\\[-1ex]") ///
numbers ///
fragment nomtitles ///
keep(daily) noobs ///
stats(N conts selected, fmt(%11.0gc 2) labels("N" "Controls Method" "Selected Controls")) ///
replace label unstack ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-.6mm}")))

esttab est4 est5 est6 using "output/tables/ITT daily contract controls.tex", ///
posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel B: Take-Up Any Insurance}} \\\\[-1ex]") ///
fragment append ///
nomtitles nonumbers nolines ///
prefoot("\hline") ///
keep(daily) noobs ///
label unstack ///
stats(N conts selected, fmt(%11.0gc 2) labels("N" "Controls Method" "Selected Controls")) ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-.6mm}")))	
	
esttab est7 est8 est9  using "output/tables/ITT daily contract controls.tex", ///
posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel C: Days with Coverage through Experiment}} \\\\[-1ex]") ///
fragment append ///
nomtitles nonumbers nolines ///
prefoot("\hline") ///
keep(daily) noobs ///
label unstack ///
stats(N conts selected, fmt(%11.0gc 2) labels("N" "Controls Method" "Selected Controls")) ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-.6mm}")))	
	
esttab est10 est11 est12 using "output/tables/ITT daily contract controls.tex", ///
posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel D: Days with Any Coverage}} \\\\[-1ex]") ///
fragment append ///
nomtitles nonumbers nolines ///
prefoot("\hline") postfoot("\hline\hline \end{tabular}") ///
keep(daily) ///
label unstack ///
stats(N conts selected, fmt(%11.0gc 2) labels("N" "Controls Method" "Selected Controls")) ///
cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
	p(par([ ]) fmt(3) label("}\vspace{-.6mm}")))
	

********************************************************************************
*** ITT interacted with income insight, vehicle value, credit constrained

*generate interacting variables
replace income_insight_score = income_insight_score*1000
qui summ income_insight_score, det
gen income_insight_scorem = income_insight_score < `r(p50)'
replace income_insight_scorem = . if mi(income_insight_score)

gen isconstrainedm = isconstrained
replace isconstrained = isconstrained*100

label variable isconstrainedm "Credit Constrained"
label variable income_insight_scorem "Below Median Income Insight Score"


local ints = "income_insight_score isconstrained"

eststo clear

local i=1
foreach int of local ints {
	local outcomes ///
	"bound anybound7 totdayscoverage90 daysnonzeroreserve90cr"
	foreach var of varlist `outcomes' {
		eststo est`i':  reg `var' i.daily##i.`int'm, r
		quietly summ `int', det
		if inlist(`i', 1,5,9) & "`int'"!="income_insight_score" estadd scalar mean = r(mean)
		if inlist(`i', 1,5,9) & "`int'"=="income_insight_score" estadd scalar median = r(p50)
		if inlist(`i', 1,5,9) estadd scalar n = r(N)
		local i = `i'+1
	}			
	local i4=`i'-4
	local i3=`i'-3
	local i2=`i'-2
	local i1=`i'-1
}

esttab est1 est2 est3 est4 using "output/tables/ITT daily contract by income and credit.tex", tex ///
	prehead("\begin{tabular}{l*{5}{c}} \hline\hline") ///
	posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel A: Income Insight Score}} \\\\[-1ex]") ///
	fragment label ///
	replace nomtitle gaps unstack noomitted nobaselevels ///
	stats(n median, fmt(%11.0gc %12.3gc) labels("N" "Median Income Insight Score")) ///
	cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
		p(par([ ]) fmt(3) label("}\vspace{-.9mm}"))) ///
	varwidth(25) wrap   /// 
	mgroups("Take-Up" "Days with Coverage" "Days Insured" "Insured End of Study", ///
		pattern(1 1 1 1))

esttab est5 est6 est7 est8 using "output/tables/ITT daily contract by income and credit.tex", tex ///
	posthead("\hline \\ \multicolumn{5}{c}{\textbf{Panel B: Credit Constrained}} \\\\[-1ex]") ///
	fragment append ///
	nomtitles nonumbers nolines noomitted nobaselevels ///
	prefoot("\hline") postfoot("\hline\hline \end{tabular}") ///
	nomtitle label gaps unstack ///
	stats(n mean, fmt(%11.0gc %12.3gc) labels("N" "Credit Constrained")) ///
	cells(b(fmt(2) label("\phantom{")) se(par fmt(2) label(" ")) ///
		p(par([ ]) fmt(3) label("}\vspace{-.9mm}"))) ///
	varwidth(25) wrap


	
********************************************************************************
*** ITT barcharts


*** hugo outcomes by price
preserve
	keep id daily bound threemonther daysnonzeroreserve90 totdayscoverage90 price
	local outcomes = "bound threemonther daysnonzeroreserve90 totdayscoverage90"
	local prices = "low base high"
	foreach var of varlist `outcomes' {
		rename `var' val`var'
	}

	reshape long val, i(id daily) j(type) string
	gen lcl = .
	gen ucl = .
	foreach price of local prices {
		foreach outcome of local outcomes {
			reg val daily if type=="`outcome'" & (price=="`price'" | daily==0), r
			local lcl = _b[_cons] + _b[daily] - invttail(e(df_r), .025) * _se[daily]
			local ucl = _b[_cons] + _b[daily] + invttail(e(df_r), .025) * _se[daily]
			replace lcl = `lcl' if type=="`outcome'" & daily==1 & price=="`price'"
			replace ucl = `ucl' if type=="`outcome'" & daily==1 & price=="`price'"
		}
	}
	collapse (mean) meanval = val (sd) sdval = val (count) n=val, by(daily price type lcl ucl)
	gen loval = meanval - invttail(n-1,0.025)*(sdval/sqrt(n))
	gen hival = meanval + invttail(n-1,0.025)*(sdval/sqrt(n))

	gen dtype = 2*daily
	replace dtype = dtype + 1.5 if price=="base"
	replace dtype = dtype + 3 if price=="high"

	local j=1
	local prices = "control low base high"
	foreach outcome of local outcomes {
		foreach price of local prices {
			if "`price'"=="control" qui summ meanval if type=="`outcome'" & daily==0
			if "`price'"!="control" qui summ meanval if type=="`outcome'" & daily==1 & price=="`price'"
			local m`j' = round(`r(mean)',.01)
			if `m`j''>10 local m`j' = round(`m`j'',.1)
			local m`j' = substr("`m`j''", 1,4)
			local j=`j'+1
		}
	}

	graph twoway (bar meanval dtype if type=="bound" & daily==0, col(navy) ///
			text(`=`m1'+.1' 0.05 "`m1'", place(n) size(small))) ///
		(bar meanval dtype if type=="bound" & daily==1 & price=="low", ///
			col(maroon%60) text(`=`m2'+.1' 2.05 "`m2'", place(ne) size(small))) ///
		(bar meanval dtype if type=="bound" & daily==1 & price=="base", ///
			col(maroon%80) text(`=`m3'+.1' 3.55 "`m3'", place(ne) size(small))) ///
		(bar meanval dtype if type=="bound" & daily==1 & price=="high", ///
			col(maroon%100) text(`=`m4'+.1' 5.05 "`m4'", place(ne) size(small))) ///
		(rcap ucl lcl dtype if type=="bound", ylab(0(10)30) legend(off) ///
			xtitle("") xlab(-1 " " 0 "Traditional" 2 "Low" ///
				3.5 `" "Base" "Pay-As-You-Go Price" "' ///
				5 "High" 6 " ", notick nogrid) ///
		ytitle("") subtitle("Take-Up (%)", pos(11) span))
	graph save one.gph, replace
	graph twoway (bar meanval dtype if type=="threemonther" & daily==0, col(navy) ///
			text(`=`m5'+.05' 0.05 "`m5'", place(n) size(small))) ///
		(bar meanval dtype if type=="threemonther" & daily==1 & price=="low", ///
			col(maroon%60) text(`=`m6'+.05' 2.05 "`m6'", place(ne) size(small))) ///
		(bar meanval dtype if type=="threemonther" & daily==1 & price=="base", ///
			col(maroon%80) text(`=`m7'+.05' 3.55 "`m7'", place(ne) size(small))) ///
		(bar meanval dtype if type=="threemonther" & daily==1 & price=="high", ///
			col(maroon%100) text(`=`m8'+.05' 5.05 "`m8'", place(ne) size(small))) ///
		(rcap ucl lcl dtype if type=="threemonther", ylab(0(5)15) legend(off) ///
			xtitle("") xlab(-1 " " 0 "Traditional" 2 "Low" ///
				3.5 `" "Base" "Pay-As-You-Go Price" "' ///
				5 "High" 6 " ", notick nogrid) ///
		ytitle("") subtitle("Insured Full Period (%)", pos(11) span))
	graph save two.gph, replace
	graph twoway (bar meanval dtype if type=="daysnonzeroreserve90" & daily==0, col(navy) ///
			text(`=`m9'+.05' 0.05 "`m9'", place(n) size(small))) ///
		(bar meanval dtype if type=="daysnonzeroreserve90" & daily==1 & price=="low", ///
			col(maroon%60) text(`=`m10'+.05' 2.05 "`m10'", place(ne) size(small))) ///
		(bar meanval dtype if type=="daysnonzeroreserve90" & daily==1 & price=="base", ///
			col(maroon%80) text(`=`m11'+.05' 3.55 "`m11'", place(ne) size(small))) ///
		(bar meanval dtype if type=="daysnonzeroreserve90" & daily==1 & price=="high", ///
			col(maroon%100) text(`=`m12'+.05' 5.05 "`m12'", place(ne) size(small))) ///
		(rcap ucl lcl dtype if type=="daysnonzeroreserve90", ylab(0(5)15) legend(off) ///
			xtitle("") xlab(-1 " " 0 "Traditional" 2 "Low" ///
				3.5 `" "Base" "Pay-As-You-Go Price" "' ///
				5 "High" 6 " ", notick nogrid) ///
		ytitle("") subtitle("Days with Coverage", pos(11) span))
	graph save three.gph, replace
	graph twoway (bar meanval dtype if type=="totdayscoverage90" & daily==0, col(navy) ///
			text(`=`m13'+.05' 0.05 "`m13'", place(n) size(small))) ///
		(bar meanval dtype if type=="totdayscoverage90" & daily==1 & price=="low", ///
			col(maroon%60) text(`=`m14'+.05' 2.05 "`m14'", place(ne) size(small))) ///
		(bar meanval dtype if type=="totdayscoverage90" & daily==1 & price=="base", ///
			col(maroon%80) text(`=`m15'+.05' 3.55 "`m15'", place(ne) size(small))) ///
		(bar meanval dtype if type=="totdayscoverage90" & daily==1 & price=="high", ///
			col(maroon%100) text(`=`m16'+.05' 5.05 "`m16'", place(ne) size(small))) ///
		(rcap ucl lcl dtype if type=="totdayscoverage90", ylab(0(5)15) legend(off) ///
			xtitle("") xlab(-1 " " 0 "Traditional" 2 "Low" ///
				3.5 `" "Base" "Pay-As-You-Go Price" "' ///
				5 "High" 6 " ", notick nogrid) ///
		ytitle("") subtitle("Days Insured", pos(11) span))
	graph save four.gph, replace

	graph combine one.gph two.gph three.gph four.gph, cols(2) iscale(0.8) imargin(small)
	graph export "output/figures/ITT daily contract bars fourpanel prices.pdf", replace
restore

erase one.gph
erase two.gph
cap erase three.gph
cap erase four.gph
